/**
 * 
 */
package com.jdy.haoduoai.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.jdy.haoduoai.event.OnTextSelectEvent;
import com.jdy.haoduoai.model.TipWordCategoryItem;
import com.jdy.haoduoai.util.Tools;
import com.jdy.haoduoai.view.WordWrapView;
import com.ycsj.goldmedalnewconcept.R;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.List;

//import com.jdy.haoduoai.R;

/**
 * @author zp 2016-5-18 下午1:45:15
 */
public class TagsFragment extends Fragment {

  @SuppressWarnings({ "unused", "unchecked" })
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.frequently_used_words_layout, container,
        false);

    WordWrapView wwv = (WordWrapView) v.findViewById(R.id.fuw_record_view);

    ScrollView scv = (ScrollView) v.findViewById(R.id.root_scroll);

    List<TipWordCategoryItem> twList = (List<TipWordCategoryItem>) getArguments()
        .getSerializable("tipwordlist");

    // String[] wordList = getArguments().getStringArray("msg");

    int i = twList.size() - 1;
    for (; i >= 0; i--) {
      String c = twList.get(i).name;
      addWord(wwv, c);
    }

    return v;
  }

  private void addWord(WordWrapView wwv, final String content) {
    TextView textview = new TextView(getActivity());
    textview.setText(content);
    textview.setTextSize(14);
    textview.setMaxEms(20);
    textview.setSingleLine();
    textview.setEllipsize(TextUtils.TruncateAt.valueOf("END"));

    textview.setTextColor(Color.argb(255, 43, 172, 252));
    textview.setBackgroundResource(R.drawable.ellipse_shape_blue);
    int padding = Tools.getPixelByDip(getActivity(), 20);
    textview.setPadding(padding, padding, padding, padding);

    textview
        .setOnTouchListener(new com.jdy.haoduoai.view.CustomTouchListener());
    textview.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {

        EventBus.getDefault().post(new OnTextSelectEvent(content));
      }
    });
    wwv.addView(textview);
  }

  public static TagsFragment newInstance(
  /* String[] text */List<TipWordCategoryItem> text) {

    TagsFragment f = new TagsFragment();

    Bundle b = new Bundle();

    // b.putStringArray("msg", text);
    b.putSerializable("tipwordlist", (Serializable) text);
    f.setArguments(b);

    return f;
  }
}